#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
#include<vector>

using namespace std;

class Solution {
    typedef pair<int, int> PII;

public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size(), ret = 0;
        int dr[4] = { -1, 1, 0, 0 }, dc[4] = { 0, 0, -1, 1 };
        vector<vector<bool>> vis(m, vector<bool>(n, 0));
        queue<PII> q;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1 && !vis[i][j]) {
                    int s = 0;
                    vis[i][j] = 1;
                    q.push({ i, j });
                    while (!q.empty()) {
                        auto [r, c] = q.front();
                        q.pop();
                        s++;
                        for (int i = 0; i < 4; ++i) {
                            int nr = r + dr[i], nc = c + dc[i];
                            if (nr >= 0 && nr < m && nc >= 0 && nc < n &&
                                !vis[nr][nc] && grid[nr][nc] == 1) {
                                q.push({ nr, nc });
                                vis[nr][nc] = 1;
                            }
                        }
                    }
                    ret = max(ret, s);
                }
            }
        }

        return ret;
    }
};

//int main() {
//    Solution s;
//    vector<vector<int>> grid = { {0,0,1,0,0,0,0,1,0,0,0,0,0},
//								 {0,0,0,0,0,0,0,1,1,1,0,0,0},
//								 {0,1,1,0,1,0,0,0,0,0,0,0,0},
//								 {0,1,0,0,1,1,0,0,1,0,1,0,0},
//								 {0,1,0,0,1,1,0,0,1,1,1,0,0},
//								 {0,0,0,0,0,0,0,0,0,0,1,0,0},
//								 {0,0,0,0,0,0,0,1,1,1,0,0,0},
//								 {0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1} };
//    cout << s.maxAreaOfIsland(grid) << endl;
//    return 0;
//    }